package com.my.basics.demo.demo02;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


/**
 * @author mars
 * @date 2025-04-12 09:34
 */
public class TimeClient{
    private final String host;
    private final int port;

    public TimeClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start() {
       try(var group = new NioEventLoopGroup()) {
           var bootstrap = new Bootstrap();
           bootstrap.group(group)
                   .channel(NioSocketChannel.class)
                   .option(ChannelOption.TCP_NODELAY, true)
                   .handler(new ChannelInitializer<SocketChannel>() {
                       @Override
                       protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new TimeClientHandler());
                       }
                   });
           // 发起异步连接操作
           ChannelFuture future = bootstrap.connect(host, port).syncUninterruptibly();
           // 等待客户端链路关闭
           future.channel().closeFuture().syncUninterruptibly();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

    public static void main(String[] args) {
        new TimeClient("127.0.0.1", 8100).start();
    }
}
